package com.boolib.shopdfs.service.impl;


import com.boolib.commons.Const;
import com.boolib.commons.entity.ServerResponse;
import com.boolib.commons.entity.WangEditorResponse;
import com.boolib.commons.util.ImageFileUtil;
import com.boolib.shopdfs.commons.FastDFSClient;
import com.boolib.shopdfs.service.IDfsService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Service
public class DfsServiceImpl implements IDfsService {


    @Value("${imageHost}")
    public String imgHost;


    FastDFSClient fastDFSClient;

    public DfsServiceImpl(){
        try {
            fastDFSClient = new FastDFSClient(Const.Dfs.DFS_CONF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public ServerResponse uploadImage(MultipartFile file) {

        if(file.isEmpty() ){
            return ServerResponse.createByErrorMessage("ERROR：文件上传不能空");
        }


        try {
            if(ImageFileUtil.isImage(file)){

                FastDFSClient fastDFSClient = new FastDFSClient(Const.Dfs.DFS_CONF);

                String ext = FilenameUtils.getExtension(file.getOriginalFilename());


                String filePath = fastDFSClient.uploadFile(  file.getBytes(),ext);


                return ServerResponse.createBySuccess(new StringBuilder(imgHost).append("/").append(filePath));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ServerResponse.createByErrorMessage("该文件不是Image类型");



    }

    @Override
    public WangEditorResponse editorUpload(MultipartFile file) {

        ServerResponse response = uploadImage(file);

        if(response.isSuccess()) {


            return WangEditorResponse.createBySuccess(response.getData());

        }

         return WangEditorResponse.createBySuccess(response.getData());
    }



    @Override
    public ServerResponse delete(String uri) {


        Integer resultCount =  fastDFSClient.delete_file(uri);


        if(resultCount==0){
            return ServerResponse.createBySuccess("删除成功");
        }


        return ServerResponse.createByErrorMessage("删除失败");


    }

    @Override
    public ResponseEntity<byte[]> download(String uri) {

        String ext = new StringBuffer().append(".").append(FilenameUtils.getExtension(uri)).toString();



        byte[] bytes = fastDFSClient.download_bytes(uri);

        HttpHeaders httpHeaders = new HttpHeaders();

        //这句话是重点， 让文件以附件的形式下载
        httpHeaders.setContentDispositionFormData("attachment", new StringBuffer().append(UUID.randomUUID().toString()).append(ext).toString());

        //只是告诉浏览器返回的内容类型
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ResponseEntity responseEntity = new ResponseEntity(bytes,httpHeaders,HttpStatus.CREATED);

        return responseEntity;

    }
}
